﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zzva.Common;
using System.Reflection;



namespace Zzva.ESB
{
    public class TSysBuhuch
    {

        private static TSysBuhuch mvarSysBuhuch;
        private object   mvarConnect;


        protected  TSysBuhuch(object pConnect)
        {
            try { mvarConnect = pConnect; }
            catch (Exception e) { throw e; }
            finally { }
        }


        public static TSysBuhuch GetSystem()
        {
                try
                {
                    if (mvarSysBuhuch == null) { mvarSysBuhuch = GetSysBuhuch(); }

                    return mvarSysBuhuch;
                }

                catch (Exception e) { throw e; }
                finally { }
        }


        private static TSysBuhuch GetSysBuhuch()
        {

            object lConnect;
            object[] ParamSys1C = new object[3];
            string lConnectStr;
            System.Type Sys1cType;
            object RMTrade;
            bool Result;
            TSysBuhuch lSysBuhuch;

            try
            {
                Sys1cType = Type.GetTypeFromProgID("V77.Application");
                lConnect = Activator.CreateInstance(Sys1cType);
                RMTrade = Sys1cType.InvokeMember("RMTrade",
                                 BindingFlags.Public | BindingFlags.InvokeMethod, null, lConnect, null);

                ParamSys1C[0] = RMTrade;

                //достаем Connect из установок
                ////Connect = RegisterLokal.GetSetting("Common", "SysBuhuchConnect");
                ////Connect = @"\\buh-srv\1C_Data\Украина2005\ZVA2007\";
                ////Connect = @"\\buh-srv\1C_Data\Украина2005\ZVA2007_Test\";
                lConnectStr = @"E:\1C_Data\Украина2005\ZVA2007\";
                lConnectStr = @"/d" + @lConnectStr + " /n" + "SysCalcul" + " /p" + "11205";


                ParamSys1C[1] = lConnectStr;
                ParamSys1C[2] = @"NO_SPLASH_SHOW";

                Result = (bool)Sys1cType.InvokeMember(@"Initialize",
                    BindingFlags.Public | BindingFlags.InvokeMethod, null, lConnect, ParamSys1C);

                if (Result == false) { throw (new Zzva.Common.CommonException("Не можем подключится к системе 1СБухгалтерия")); }

                lSysBuhuch = new TSysBuhuch(lConnect);
                return lSysBuhuch;

            }
            catch (Exception e) { throw e; }
            finally { }
        }


        public bool GetNomencl(string Obozn, out string Naim, out string Ei)
        {
            try
            {
                object ListNomencl;
                object[] ListNomenclParam = new object[1];
                double ResultListNomenclItem;
                object ListNomenclItem;
                object[] ListNomenclItemParam = new object[1];
                bool Result;
                string lNaim;
                string lEi;
                object EiC1;

                ListNomenclParam[0] = @"Справочник.ТМЦ";
                ListNomencl = mvarConnect.GetType().InvokeMember(@"CreateObject",
                       BindingFlags.Public | BindingFlags.InvokeMethod, null, mvarConnect, ListNomenclParam);

                if (ListNomencl == null) { throw (new Zzva.Common.CommonException("Не можем открыть справочник TMC")); }


                ListNomenclItemParam[0] = Obozn;

                ResultListNomenclItem = (Double)ListNomencl.GetType().InvokeMember(@"FindByCode",
                           BindingFlags.Public | BindingFlags.InvokeMethod, null, ListNomencl, ListNomenclItemParam);

                if (ResultListNomenclItem == 1)
                {
                    ListNomenclItem = ListNomencl.GetType().InvokeMember(@"CurrentItem",
                           BindingFlags.Public | BindingFlags.InvokeMethod, null, ListNomencl, null);

                    lNaim = (string)ListNomenclItem.GetType().InvokeMember(@"ПолнНаименование",
                                   BindingFlags.Public | BindingFlags.GetProperty, null, ListNomenclItem, null);
                    Naim = lNaim.TrimEnd();

                    EiC1 = (object)ListNomenclItem.GetType().InvokeMember(@"БазЕдиница",
                                   BindingFlags.Public | BindingFlags.GetProperty, null, ListNomenclItem, null);
                    lEi = (string)EiC1.GetType().InvokeMember(@"Identifier",
                                   BindingFlags.Public | BindingFlags.InvokeMethod, null, EiC1, null);
                    Ei = lEi.TrimEnd();


                    Result = true;
                }
                else
                {
                    Naim = "";
                    Ei = "";
                    Result = false;
                }

                return Result;

            }

            catch (Exception e) { throw e; }
            finally { }
        }
  




    }
}
